# Coding = UTF-8
import pandas as pd


def wt_analyze(value_list, min_times, max_times):  # value_list 为所需分析的列数组，times为所需判断的倍数，如6倍
    length = len(value_list) - 1  # The length of list 获得列的长度
    w_value_ar = []  # The value of wave 波峰的数值
    w_tag_ar = []  # The tag of wave 波峰数值对应的坐标(tag)
    t_value_ar = []  # The value of trough 波谷的数值
    t_tag_ar = []  # The tag of wave 波谷数值对应的坐标(tag)
    tag_3 = 0
    for tag_1 in range(1, length):
        if value_list[tag_1 - 1] - value_list[tag_1] < 0 and value_list[tag_1] - value_list[tag_1 + 1] > 0:
            w_value_ar.append(value_list[tag_1])
            w_tag_ar.append(tag_1 + 1)
        elif value_list[tag_1 - 1] - value_list[tag_1] > 0 and value_list[tag_1] - value_list[tag_1 + 1] < 0:
            t_value_ar.append(value_list[tag_1])
            t_tag_ar.append(tag_1 + 1)
    length_demand = len(w_value_ar)
    array = [[[] for i in range(3)] for j in range(15)]
    array_result = []
    # Find the wave and trough
    for tag_2 in range(0, length_demand):
        left_tag = tag_2
        right_tag = tag_2
        t_f_1 = 0
        t_f_2 = 0
        temple_left_lists = []
        temple_right_lists = []
        while left_tag != 0:
            if t_value_ar[left_tag] * max_times > w_value_ar[tag_2] > t_value_ar[left_tag] * min_times:
                t_f_1 = 1
                temple_left_lists.append(left_tag)
            left_tag = left_tag - 1
        while right_tag != length_demand:
            if t_value_ar[right_tag] * max_times > w_value_ar[tag_2] > t_value_ar[right_tag] * min_times:
                t_f_2 = 1
                temple_right_lists.append(right_tag)
            right_tag = right_tag + 1
        if t_f_1 == t_f_2 == 1:
            array[tag_3][0].extend(temple_left_lists)
            array[tag_3][1].append(tag_2)
            array[tag_3][2].extend(temple_right_lists)
            tag_3 = tag_3 + 1
    # Getting the demand of array
    tag_count = len(array[0])  # 有多少个对于tag可有左右坐标的
    for three_demen in range(tag_count):
        length_two = len(array[three_demen][0])
        length_two_1 = len(array[three_demen][2])
        for two_demen in range(length_two):
            for two_demn_1 in range(length_two_1):
                float_1 = w_value_ar[array[three_demen][1][0]] / t_value_ar[array[three_demen][0][two_demen]]
                float_2 = w_value_ar[array[three_demen][1][0]] / t_value_ar[array[three_demen][2][two_demn_1]]
                array_result.extend([
                 [w_tag_ar[array[three_demen][1][0]], w_value_ar[array[three_demen][1][0]]],
                 [t_tag_ar[array[three_demen][0][two_demen]], t_value_ar[array[three_demen][0][two_demen]], float_1],
                 [t_tag_ar[array[three_demen][2][two_demn_1]], t_value_ar[array[three_demen][2][two_demn_1]], float_2]
                ])
    print(array_result)
    return w_value_ar, w_tag_ar, t_value_ar, t_tag_ar
    # [[27, 15990000], [7, 1671000, 9.569120287253142], [37, 2503000, 6.388333999200959], [44, 44840000],...]
    # 输出结果中[[波峰][左波谷][右波谷]]每三组表示一组


df2 = pd.read_csv('result.csv')
col = df2.iloc[:, 4]  # 取表中的第4列的所有值
w_value_arrs = col.values
wt_analyze(w_value_arrs, 6, 10)  # '6'为需要取的波峰与波谷之间的倍数
